package com.hm.cms.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hm.cms.auto.SystemEnvConfigProperties;
import com.hm.cms.auto.su.R.SystemConfig;
import com.hm.cms.def.AdvertSiteEnum;
import com.hm.cms.def.ExtLlinkEnum;
import com.hm.cms.service.AdvertService;
import com.hm.cms.service.ArticleService;
import com.hm.cms.service.ExtLinkService;
import com.hm.cms.service.TermTaxonomyService;
import com.hm.cms.service.TermsService;
import com.hm.cms.vo.HcmsArticleVo;
import com.hm.cms.vo.HcmsHomePageVo;
import com.hm.cms.vo.HcmsPageAdvertVo;
import com.hm.cms.vo.HcmsPageBaseInfoVo;
import com.hm.cms.vo.HcmsPageExtLinkVo;
import com.hm.cms.vo.HcmsTermTaxonomyVo;
import com.hm.cms.vo.HcmsTermsVo;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageInfoParam;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.EncryptUtil.Base64;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * @author shishun.wang
 * @date 下午5:35:47 2017年10月25日
 * @version 1.0
 * @describe 分类连接处理
 */
@Controller
@RequestMapping("o_term")
public class TermController {

	@Autowired
	private TermsService termsService;

	@Autowired
	private AdvertService advertService;

	@Autowired
	private ExtLinkService extLinkService;

	@Autowired
	private TermTaxonomyService termTaxonomyService;

	@Autowired
	private ArticleService articleService;

	@Autowired
	private SystemEnvConfigProperties systemEnvConfigProperties;

	@RequestMapping(value = "/{termTayCode}/{termId}", method = RequestMethod.GET)
	public String queryTermArticles(@PathVariable("termTayCode") String termTayCode,
			@PathVariable("termId") String termId, Model model) {
		this.initBaseInfo(model, termId);
		this.setHeadPageAdvert(AdvertSiteEnum.HOME_HEAD_SLIDE, model);
		this.setExtLink(model);

		HcmsTermTaxonomyVo termTaxonomyVo = termTaxonomyService.getTermTaxonomysByCode(termTayCode);
		model.addAttribute("termTay", termTaxonomyVo);

		HcmsTermsVo termsVo = termsService.getHcmsTermsById(termId);
		model.addAttribute("term", termsVo);
		model.addAttribute("moreArticlCatalogUri", "o_term/pagging/" + termTayCode + "/" + termId);

		HcmsHomePageVo homePageVo = new HcmsHomePageVo();
		{
			homePageVo.setWebPageNavs(termTaxonomyService.loadSysNavs());
		}
		model.addAttribute("homePageInfo", homePageVo);
		model.addAttribute("basePath", systemEnvConfigProperties.getStaticHtmlDomainName());

		return "article/article_list";
	}

	@RequestMapping(value = "/pagging/{termTayCode}/{termId}", method = RequestMethod.GET)
	public void paggingTermArticles(@PathVariable("termTayCode") String termTayCode,
			@PathVariable("termId") String termId, @RequestParam(name = "pageNumber") Integer pageNumber,
			HttpServletResponse response) {

		try {
			PageInfo<HcmsArticleVo> pageInfo = articleService
					.queryArticle(new PageInfoParam(pageNumber, SystemConfig.ARTICLE_CATALOG_SHOW_SIZE), null, termId);
			if (CommonUtil.isEmpty(pageInfo)) {
				response.getWriter().write("没有更多内容了....");
				return;
			}

			HcmsPageBaseInfoVo baseInfoVo = new HcmsPageBaseInfoVo();
			baseInfoVo.newestArticle(pageInfo);
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			// 这句话的意思，是告诉servlet用UTF-8转码，而不是用默认的ISO8859
			response.setCharacterEncoding("UTF-8");

			Resource resource = new ClassPathResource("/templates/common");
			File file = resource.getFile();
			Configuration configuration = new Configuration();
			configuration.setDirectoryForTemplateLoading(file);
			configuration.setDefaultEncoding("UTF-8");

			Template template = configuration.getTemplate("article_catalog_view.ftl");
			Map<String, Object> mapper = new HashMap<String, Object>();
			mapper.put("baseInfo", baseInfoVo);
			mapper.put("basePath", systemEnvConfigProperties.getStaticHtmlDomainName());

			template.process(mapper, response.getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initBaseInfo(Model model, String termId) {
		HcmsPageBaseInfoVo baseInfoVo = new HcmsPageBaseInfoVo();
		{
			baseInfoVo.setOpenLogin(systemEnvConfigProperties.isOpenLogin());
			baseInfoVo.setSiteStatement(
					new String(Base64.decode(systemEnvConfigProperties.getSiteStatement().toCharArray())));
			// baseInfoVo.setAttQrCode(new
			// String(Base64.decode(systemEnvConfigProperties.getAttentionQrCode().toCharArray())));
			baseInfoVo.setSiteFtInfo(
					new String(Base64.decode(systemEnvConfigProperties.getSiteFooterInfo().toCharArray())));
			baseInfoVo.setSiteShare(new String(Base64.decode(systemEnvConfigProperties.getSiteShare().toCharArray())));
			baseInfoVo.termTaxonomy(termTaxonomyService.loadHcmsTermTaxonomys());

			baseInfoVo.newestArticle(articleService
					.queryArticle(new PageInfoParam(1, SystemConfig.ARTICLE_CATALOG_SHOW_SIZE), null, termId));
			baseInfoVo.hotArticle(
					articleService.queryArticle(new PageInfoParam(1, SystemConfig.TAG_SHOW_SIZE), null, null));
		}

		model.addAttribute("baseInfo", baseInfoVo);
	}

	private void setExtLink(Model model) {
		model.addAttribute("pageExtLlink",
				new HcmsPageExtLinkVo().friendshipLink(extLinkService.loadExtLinks(ExtLlinkEnum.FRIENDSHIP_LINK))
						.onlineToolLink(extLinkService.loadExtLinks(ExtLlinkEnum.ONLINE_TOOL_LINK)));
	}

	private void setHeadPageAdvert(AdvertSiteEnum advertSite, Model model) {
		model.addAttribute("pageAdvert", new HcmsPageAdvertVo().headSlides(advertService.loadPageAdvert(advertSite)));
	}
}
